home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Toolkit / Songbird 0.1 / Songbird_0_1_0.exe / chrome / content / watch_folders.js < prev    next >
Text File  |  2006-02-07  |  11KB  |  381 lines

  1. /*
  2. //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright⌐ 2006 Pioneers of the Inevitable LLC
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the ôGPLö).
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an ôAS ISö basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. var wfMediaLibrary = null;
  28. var wfMediaScan = null;
  29. var wfMediaScanQuery = null;
  30. var wfQuery = null;
  31.  
  32. //Default to 10 minute update interval.
  33. var wfDefaultInterval = 10;
  34. var wfInterval = 0;
  35.  
  36. var wfIsProcessing = false;
  37. var wfMaxTracksBeforeProcess = 99;
  38.  
  39. var wfCurrentFolder = 0;
  40. var wfCurrentFolderList = new Array();
  41.  
  42. var wfCurrentFile = 0;
  43.  
  44. var wfWakeUpTimer = null;
  45. var wfPollScanTimer = 0;
  46. var wfMediaLibraryAddTimer = 0;
  47.  
  48. function IsMediaUrl( the_url )
  49. {
  50.   if ( ( the_url.indexOf ) && 
  51.         (
  52.           // Protocols at the beginning
  53.           ( the_url.indexOf( "mms:" ) == 0 ) || 
  54.           ( the_url.indexOf( "rtsp:" ) == 0 ) || 
  55.           // File extensions at the end
  56.           ( the_url.indexOf( ".pls" ) != -1 ) || 
  57.           ( the_url.indexOf( ".m3u" ) == ( the_url.length - 4 ) ) || 
  58. //          ( the_url.indexOf( ".rm" ) == ( the_url.length - 3 ) ) || 
  59. //          ( the_url.indexOf( ".ram" ) == ( the_url.length - 4 ) ) || 
  60. //          ( the_url.indexOf( ".smil" ) == ( the_url.length - 5 ) ) || 
  61.           ( the_url.indexOf( ".mp3" ) == ( the_url.length - 4 ) ) ||
  62.           ( the_url.indexOf( ".ogg" ) == ( the_url.length - 4 ) ) ||
  63.           ( the_url.indexOf( ".wma" ) == ( the_url.length - 4 ) ) ||
  64.           ( the_url.indexOf( ".wmv" ) == ( the_url.length - 4 ) ) ||
  65.           ( the_url.indexOf( ".asx" ) == ( the_url.length - 4 ) ) ||
  66.           ( the_url.indexOf( ".asf" ) == ( the_url.length - 4 ) ) ||
  67.           ( the_url.indexOf( ".avi" ) == ( the_url.length - 4 ) ) ||
  68.           ( the_url.indexOf( ".mov" ) == ( the_url.length - 4 ) ) ||
  69.           ( the_url.indexOf( ".mp4" ) == ( the_url.length - 4 ) )
  70.         )
  71.       )
  72.   {
  73.     return true;
  74.   }
  75.   return false;
  76. }
  77.  
  78. function WFInit()
  79. {
  80.   const MediaLibrary = new Components.Constructor("@songbird.org/Songbird/MediaLibrary;1", "sbIMediaLibrary");
  81.   
  82.   if(!wfMediaLibrary)
  83.     wfMediaLibrary = new MediaLibrary();
  84.  
  85.   wfMediaScan = new sbIMediaScan();
  86.   wfMediaScanQuery = new sbIMediaScanQuery();
  87.   wfQuery = new sbIDatabaseQuery();
  88.   
  89.   wfQuery.SetAsyncQuery(true);
  90.   wfQuery.SetDatabaseGUID("songbird");
  91.   wfMediaLibrary.SetQueryObject(wfQuery);
  92.   
  93.   wfManager.CreateWatchFolderManager();
  94.   
  95.   if(wfWakeUpTimer)
  96.     clearInterval(wfWakeUpTimer);
  97.     
  98.   wfWakeUpTimer = 0;
  99.   
  100.   setTimeout( onWFWakeUpScan, 30 * 1000 );
  101. }
  102.  
  103. function onWFWakeUpScan()
  104. {
  105.   if ( ! wfWakeUpTimer )
  106.   {
  107.     wfWakeUpTimer = setInterval(onWFWakeUpScan, 10000);
  108.   }
  109.     
  110.   if(wfCurrentFolderList.length == 0)
  111.   {
  112.     var aFolders = wfManager.GetWatchFolders();
  113.     if(aFolders && aFolders.length)
  114.     {
  115.       wfCurrentFolder = 0;
  116.       wfCurrentFolderList = aFolders;
  117.       
  118.       wfMediaScanQuery.SetDirectory(aFolders[0]);
  119.       wfMediaScanQuery.SetRecurse(true);
  120.       
  121.       wfMediaScan.SubmitQuery(wfMediaScanQuery);
  122.       wfPollScanTimer = setInterval(onWFPollScan, 333);
  123.     }
  124.   }
  125. }
  126.  
  127. function onWFPollScan()
  128. {
  129.   if(!wfMediaScanQuery.IsScanning())
  130.   {
  131.     clearInterval(wfPollScanTimer);
  132.     onWFScanComplete();
  133.   }
  134. }
  135.  
  136. function onWFScanComplete()
  137. {
  138.   wfMediaLibraryAddTimer = setInterval(onWFLibraryAdd, 66);
  139. }
  140.  
  141. function onWFLibraryAdd()
  142. {
  143.   if(wfQuery.IsExecuting())
  144.     return;
  145.     
  146.   if(wfIsProcessing)
  147.   {
  148.     wfQuery.ResetQuery();
  149.     wfIsProcessing = false;
  150.   }
  151.  
  152.   var fileCount = wfMediaScanQuery.GetFileCount();
  153.   
  154.   if(wfCurrentFile < fileCount)
  155.   {
  156.     var strURL = wfMediaScanQuery.GetFilePath(wfCurrentFile);
  157.     
  158.     if(IsMediaUrl(strURL))
  159.     {
  160.       var keys = new Array( "title" );
  161.       var values = new Array();
  162.           
  163.       values.push( ConvertUrlToDisplayName( strURL ) );
  164.       
  165.       wfMediaLibrary.AddMedia(strURL, keys.length, keys, values.length, values, false, true);
  166.       
  167.       if(wfQuery.GetQueryCount() > wfMaxTracksBeforeProcess)
  168.       {
  169.         wfQuery.Execute();
  170.         wfIsProcessing = true;
  171.       }
  172.     }
  173.     
  174.     wfCurrentFile++;
  175.     
  176.     return;
  177.   }
  178.   else if(wfQuery.GetQueryCount() > 0)
  179.   {
  180.     dump("Dumping left overs. Count: " + wfQuery.GetQueryCount() + "\n");
  181.     
  182.     wfQuery.Execute();
  183.     wfIsProcessing = true;
  184.   }
  185.   else if(wfCurrentFolder + 1 < wfCurrentFolderList.length)
  186.   {
  187.     clearInterval(wfMediaLibraryAddTimer);
  188.     
  189.     wfQuery.ResetQuery();
  190.     wfCurrentFile = 0;
  191.     wfCurrentFolder++;
  192.  
  193.     wfMediaScanQuery.SetDirectory(wfCurrentFolderList[wfCurrentFolder]);
  194.     wfMediaScanQuery.SetRecurse(true);
  195.       
  196.     wfMediaScan.SubmitQuery(wfMediaScanQuery);
  197.     wfPollScanTimer = setInterval(onWFPollScan, 333);
  198.   }
  199.   else
  200.   {
  201.     clearInterval(wfMediaLibraryAddTimer);
  202.  
  203.     wfQuery.ResetQuery();
  204.     wfCurrentFile = 0;
  205.     wfCurrentFolder = 0;
  206.     wfCurrentFolderList = new Array();
  207.   }
  208.  
  209. }
  210.  
  211. function CWatchFolderManager()
  212. {
  213.   this.m_queryObj = new sbIDatabaseQuery();
  214.   
  215.   this.m_watchDBGUID = "watch_folders";
  216.   this.m_watchFolderTable = "watch_folders";
  217.   this.m_watchFolderSettingsTable = "watch_folders_settings";
  218.   
  219.   this.CreateWatchFolderManager = function CreateWatchFolderManager()
  220.   {
  221.     var watchFolderCreate = "CREATE TABLE " + this.m_watchFolderTable + " (folder TEXT UNIQUE NOT NULL)";
  222.     var watchFolderSettingsCreate = "CREATE TABLE " + this.m_watchFolderSettingsTable + " (name TEXT UNIQUE NOT NULL, value TEXT NOT NULL DEFAULT '')";
  223.     
  224.     this.m_queryObj.SetAsyncQuery(false);
  225.     this.m_queryObj.SetDatabaseGUID(this.m_watchDBGUID);
  226.     this.m_queryObj.ResetQuery();
  227.     
  228.     this.m_queryObj.AddQuery(watchFolderSettingsCreate);
  229.     this.m_queryObj.AddQuery(watchFolderCreate);    
  230.     
  231.     this.m_queryObj.Execute();
  232.   }
  233.   
  234.   this.AddWatchFolder = function AddWatchFolder(strFolder)
  235.   {
  236.     var aFolders = new Array();
  237.     aFolders.push(strFolder);
  238.     return this.AddWatchFolders(aFolders);
  239.   }
  240.   
  241.   this.AddWatchFolders = function AddWatchFolders(aFolders)
  242.   {
  243.     var folderCount = 0;
  244.     if(aFolders.length)
  245.     {
  246.       this.m_queryObj.ResetQuery();
  247.       for(var i = 0; i < aFolders.length; i++)
  248.       {
  249.         this.m_queryObj.AddQuery("INSERT OR REPLACE INTO " + this.m_watchFolderTable + " VALUES (\"" + aFolders[i] + "\")");
  250.         ++folderCount;
  251.       }
  252.       this.m_queryObj.Execute();
  253.     }
  254.     
  255.     return folderCount;
  256.   }
  257.   
  258.   this.SetWatchFolder = function SetWatchFolder(strOldFolder, strNewFolder)
  259.   {
  260.     this.m_queryObj.ResetQuery();
  261.     this.m_queryObj.AddQuery("UPDATE " + this.m_watchFolderTable + " SET folder = \"" + strNewFolder + "\" WHERE folder = \"" + strOldFolder + "\"");
  262.     this.m_queryObj.Execute();
  263.   }
  264.   
  265.   this.RemoveWatchFolder = function RemoveWatchFolder(strFolder)
  266.   {
  267.     this.m_queryObj.ResetQuery();
  268.     this.m_queryObj.AddQuery("DELETE FROM " + this.m_watchFolderTable + " WHERE folder = \"" + strFolder + "\"");
  269.     this.m_queryObj.Execute();
  270.   }
  271.  
  272.   this.RemoveWatchFolders = function RemoveWatchFolders(aFolders)
  273.   {
  274.     var folderCount = 0;
  275.     
  276.     if(aFolders.length)
  277.     {
  278.       this.m_queryObj.ResetQuery();
  279.       for(var i = 0; i < aFolders.length; i++)
  280.       {
  281.         this.m_queryObj.AddQuery("DELETE FROM " + this.m_watchFolderTable + " WHERE folder = \"" + aFolders[i] + "\"");
  282.         folderCount++;
  283.       }
  284.       this.m_queryObj.Execute();
  285.     }
  286.     
  287.     return folderCount;
  288.   }
  289.  
  290.   this.RemoveAllWatchFolders = function RemoveAllWatchFolders()
  291.   {
  292.     this.m_queryObj.ResetQuery();
  293.     this.m_queryObj.AddQuery("DELETE FROM " + this.m_watchFolderTable);
  294.     this.m_queryObj.Execute();
  295.   }
  296.  
  297.   this.GetWatchFolderCount = function GetWatchFolderCount()
  298.   {
  299.     var folderCount = 0;
  300.     this.m_queryObj.ResetQuery();
  301.     this.m_queryObj.AddQuery("SELECT COUNT(folder) FROM " + this.m_watchFolderTable);
  302.     this.m_queryObj.Execute();
  303.     
  304.     var resObj = this.m_queryObj.GetResultObject();
  305.     if(resObj.GetRowCount())
  306.       folderCount = parseInt(resObj.GetRowCell(0, 0));
  307.       
  308.     return folderCount;
  309.   }
  310.   
  311.   this.GetWatchFolders = function GetWatchFolders()
  312.   {
  313.     var aFolders = new Array();
  314.     
  315.     this.m_queryObj.ResetQuery();
  316.     this.m_queryObj.AddQuery("SELECT folder FROM " + this.m_watchFolderTable);
  317.     this.m_queryObj.Execute();
  318.     
  319.     var resObj = this.m_queryObj.GetResultObject();
  320.     var rowCount = resObj.GetRowCount();
  321.     if(rowCount)
  322.     {
  323.       for(var i = 0; i < rowCount; i++)
  324.       {
  325.         var strFolder = resObj.GetRowCellByColumn(i, "folder");
  326.         aFolders.push(strFolder);
  327.       }
  328.     }
  329.     
  330.     return aFolders;
  331.   }
  332.   
  333.   this.GetFolderScanInterval = function GetFolderScanInterval()
  334.   {
  335.     var scanInterval = 0;
  336.     
  337.     this.m_queryObj.ResetQuery();
  338.     this.m_queryObj.AddQuery("SELECT value FROM " + this.m_watchFolderSettingsTable + " WHERE name = 'scan_interval'");
  339.     this.m_queryObj.Execute();
  340.     
  341.     var resObj = this.m_queryObj.GetResultObject();
  342.     if(resObj.GetRowCount())
  343.       scanInterval = parseInt(resObj.GetRowCell(0, 0));
  344.     
  345.     return scanInterval;
  346.   }
  347.   
  348.   this.SetFolderScanInterval = function SetFolderScanInterval(scanInterval)
  349.   {
  350.     this.m_queryObj.ResetQuery();
  351.     this.m_queryObj.AddQuery("INSERT OR REPLACE INTO " + this.m_watchFolderSettingsTable + " VALUES ('scan_interval', '" + scanInterval + "')");
  352.     this.m_queryObj.Execute();
  353.   }
  354.   
  355.   this.GetFolderLastScanTime = function GetFolderLastScanTime()
  356.   {
  357.     var lastScanTime = 0;
  358.     
  359.     this.m_queryObj.ResetQuery();
  360.     this.m_queryObj.AddQuery("SELECT value FROM " + this.m_watchFolderSettingsTable + " WHERE name = 'last_scan_time'");
  361.     this.m_queryObj.Execute();
  362.     
  363.     var resObj = this.m_queryObj.GetResultObject();
  364.     if(resObj.GetRowCount())
  365.       lastScanTime = parseInt(resObj.GetRowCell(0, 0));
  366.     
  367.     return lastScanTime;
  368.   }
  369.   
  370.   this.SetFolderLastScanTime = function SetFolderLastScanTime()
  371.   {
  372.     var dNow = new Date();
  373.     this.m_queryObj.ResetQuery();
  374.     this.m_queryObj.AddQuery("INSERT OR REPLACE INTO " + this.m_watchFolderSettingsTable + " VALUES ('last_scan_time', '" + dNow.getTime() + "')");
  375.     this.m_queryObj.Execute();
  376.   }
  377. }
  378.  
  379. var wfManager = new CWatchFolderManager();
  380.  
  381.